package com.xiaotu.spider.tudou.pageprocessor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.TVRecordModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.TVProcessUtils;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 TuDouPageProcessor
 * @日期 2017年2月13日
 * @作者 高海军
 * @功能 土豆网数据爬取
 */
@Component("TuDouPageProcessor")
@Scope("prototype")
public class TuDouPageProcessor extends APageProcessor
{
	
	private static final String searchURL = "http://www.soku.com/t/nisearch/";
	
	@Override
	protected Request getStartRequest(JSONObject target)
	{
		return new Request(
				searchURL + KeywordEncode.encode(target.getString("tvName")));
	}
	
	@Override
	protected String getSpiderType()
	{
		return Constants.SpiderTaskType.TV_TUDOU;
	}
	
	@Override
	public String getPageDataType(Page page)
	{
		String URL = page.getUrl().toString();
		if (URL.contains(searchURL))
			return Constants.PageDataType.SEARCH_LIST;
		if (URL.contains("video.tudou.com/v"))
			return Constants.PageDataType.TV_RECORD;
		return Constants.PageDataType.SEARCH_LIST;
	}
	
	@Override
	public void process(Page page)
	{
		Map<String, Object> tvInfoMap = page.getRequest().getExtras();
		String dataType = this.getPageDataType(page);
		
		if (Constants.PageDataType.SEARCH_LIST.equals(dataType))
			searchListProcess(page, tvInfoMap);
		if (Constants.PageDataType.TV_RECORD.equals(dataType))
			tvRecordProcess(page, tvInfoMap);
		//searchListProcess(page, tvInfoMap);
	}
	
	
	protected void searchListProcess(Page page, Map<String, Object> tvMap)
	{
		// 搜索頁面
		List<String> links = page.getHtml()
				.xpath("//body/div[@class='sk_container']/div[@class='sk-express']/div[@class='DIR']"
						+ "/div[@class='s_dir']/div[@class='clearfix']/div[@class='s_inform']"
						+ "/div[@class='s_detail']/div[@class='s_base']/h2[@class='base_name']/a[1]/@href")
				.all();
		/*List<String> links = page.getHtml()
				.xpath("//div[@class='s_tv clearfix']/div[@class='s_poster']/div[@class='s_link']/a/@href")
				.all();*/
		
		List<String> names = page.getHtml()
				.xpath("//div[@class='s_detail']/div[@class='s_base']/h2[@class='base_name']/a[1]/@_log_title")
				.all();
		// 电视剧类型
		List<String> tvTypes = page.getHtml()
				.xpath("//div[@class='s_detail']/div[@class='s_base']/span[@class='base_type']/text()")
				.all();
		
		// div中包含属于哪个站点的信息
		final List<String> divList = page.getHtml()
				.xpath("//body/div[@class='sk_container']/div[@class='sk-express']/div[@class='DIR']/div[@Class='s_dir']")
				.all();
		/*final List<String> divList = page.getHtml()
				.xpath("//div[@class='pos_sel']/div[@class='pos_area']/span[1]/text()")
				.all();*/
		// 播放时间
		List<String> releasetimes = page.getHtml()
				.xpath("//div[@class='s_detail']/div[@class='s_base']/h2[@class='base_name']/a/span[@class!='c_highred']/text()")
				.regex(RegexUtils.REGEX_INTEGER).all();
		
		List<String> actorsSpan = page.getHtml()
				.xpath("//div[@class='s_detail']/div[@class='s_info']/span[@class='s_figure']")
				.all();
		List<List<String>> actorsList = new ArrayList<List<String>>();
		
		for (String actorSpan : actorsSpan)
		{
			
			Html actorHtml = new Html(actorSpan);
			
			actorsList.add(actorHtml.xpath("//a/text()").all());
			
		}
		
		/*if (links.size() != names.size() || links.size() != tvTypes.size()
				|| links.size() != divList.size()
				|| links.size() != releasetimes.size()
				|| links.size() != actorsList.size())
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);*/
		
		TVProcessUtils.addTVFromSearchList(page, tvMap, names, links, tvTypes,
				actorsList, null, new TVProcessUtils.TVMatcher()
				{
					@Override
					protected boolean addTV(Page page,
							Map<String, Object> tvinfoMap, String name,
							String tvType, List<String> actors,
							List<String> directors, String link, int idx)
					{
						if (TVProcessUtils.tvMatched(tvinfoMap, name,
								TVProcessUtils.getTVType(tvType), actors,
								directors)
								&& getSiteName()
										.equals(new Html(divList.get(idx))
												.xpath("//div[@class='s_inform']/div[@class='s_play']/div[@class='pos']/div[@class='pos_sel']/div[@class='pos_area']/span/text()")
												.get()))
						{
							PageUtils.addTargetRequest(page, link, tvinfoMap,
									true);
							return true;
						}
						return false;
					}
				}, this.getGetter(divList));
		
	}
	
	private void tvRecordProcess(Page page, Map<String, Object> tvInfoMap)
	{
		Html html = page.getHtml();
		List<String> playcountList = html
				.xpath("//div[@class='td-listbox__skip']/div[@class='td-listbox__b td-listbox__b--show']/div[@class='td-listbox__list']"
						+ "/div/a/div[@class='td-video__meta']/div[@class='td-video__meta__other']/span[@class='td-video__meta__num']/text()")
				.all();
		if (playcountList.size()==0)
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		int playCount = 0;
		for (String playcountStr : playcountList) {
			if(playcountStr.contains(" 次播放")){
				playcountStr =playcountStr.replace(" 次播放", "");
			}
			if(playcountStr.contains("次播放")){
				playcountStr =playcountStr.replace("次播放", "");
			}
			if(playcountStr.contains("万")){
				playcountStr = playcountStr.replace("万", "");
				playcountStr =playcountStr.replace(".", "")+"000";
				playCount =playCount+Integer.parseInt(playcountStr);
			}else if(playcountStr.contains("亿")){
				playcountStr = playcountStr.replace("亿", "");
				playcountStr =playcountStr.replace(".", "")+"0000000";
				playCount =playCount+Integer.parseInt(playcountStr);
			}else{
				playCount =playCount+Integer.parseInt(playcountStr);
			}
		}
		
		page.putField(TVRecordModel.class.getName(),
				new TVRecordModel(tvInfoMap.get("tvid") + "",
						TVProcessUtils.getPageTVName(tvInfoMap),
						Constants.SiteName.TUDOU, null,
						playCount+"",null, null,null, null,
						TVProcessUtils.getPageUpdateSet(tvInfoMap)));
	}
	
	/**
	 * 设置已更新集次
	 * @param tvInfoMap
	 * @param divObj
	 */
	private TVProcessUtils.UpdateSetGetter getGetter(final List<String> divList)
	{
		return new TVProcessUtils.UpdateSetGetter()
		{
			@Override
			public String getUpdateSet(int idx)
			{
				
				String setStr = new Html(divList.get(idx))
						.xpath("//em[@class='updateNotice']/text()").get();
				if (StringUtils.isEmpty(setStr))
					return null;
				setStr = RegexUtils.regexSearch(
						"更新至" + RegexUtils.REGEX_INTEGER + "集", setStr);
				if (StringUtils.isEmpty(setStr))
					return null;
				return RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, setStr);
			}
		};
		
	}
	
	protected String getSiteName()
	{
		return Constants.SiteName.TUDOU;
	}
}
